<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form ref="formRef" :model="formData" :rules="formRules" label-width="180px" v-loading="formLoading">
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="年份" prop="years">
            <el-date-picker v-model="formData.years" type="year" format="YYYY" value-format="YYYY" placeholder="选择年份" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="月份" prop="month">
            <el-date-picker v-model="formData.month" type="month" format="MM" value-format="MM" placeholder="选择月份" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="员工" prop="userId">
            <UserSelect v-model="formData.userId" @node-change="onUserChange" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="分配企业总户数" prop="totalEnterprises">
            <el-input v-model="formData.totalEnterprises" placeholder="请输入分配企业总户数" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="小规模户数" prop="smallScaleEnterprises">
            <el-input v-model="formData.smallScaleEnterprises" placeholder="请输入小规模户数" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="一般纳税人户数" prop="generalTaxpayerEnterprises">
            <el-input v-model="formData.generalTaxpayerEnterprises" placeholder="请输入一般纳税人户数" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="小规模分" prop="smallScaleScore">
            <el-input v-model="formData.smallScaleScore" placeholder="请输入小规模分" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="一般纳税人分" prop="generalTaxpayerScore">
            <el-input v-model="formData.generalTaxpayerScore" placeholder="请输入一般纳税人分" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="简化小规模户数" prop="simplifiedSmallScale">
            <el-input v-model="formData.simplifiedSmallScale" placeholder="请输入简化小规模户数" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="简化一般纳税人户数" prop="simplifiedGeneralTaxpayer">
            <el-input v-model="formData.simplifiedGeneralTaxpayer" placeholder="请输入简化一般纳税人户数" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="简化小规模分" prop="simplifiedSmallScaleScore">
            <el-input v-model="formData.simplifiedSmallScaleScore" placeholder="请输入简化小规模分" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="简化一般纳税人分" prop="simplifiedGeneralTaxpayerScore">
            <el-input v-model="formData.simplifiedGeneralTaxpayerScore" placeholder="请输入简化一般纳税人分" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="开票分" prop="invoiceScore">
            <el-input v-model="formData.invoiceScore" placeholder="请输入开票分" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="特殊分" prop="specialScore">
            <el-input v-model="formData.specialScore" placeholder="请输入特殊分" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="工龄分" prop="seniorityScore">
            <el-input v-model="formData.seniorityScore" placeholder="请输入工龄分" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="证书分" prop="certificateScore">
            <el-input v-model="formData.certificateScore" placeholder="请输入证书分" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="收款完成率(%)" prop="collectionCompletionRate">
            <el-input v-model="formData.collectionCompletionRate" placeholder="请输入收款完成率(%)" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收款扣分" prop="collectionDeduction">
            <el-input v-model="formData.collectionDeduction" placeholder="请输入收款扣分" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="投诉扣分" prop="complaintDeduction">
            <el-input v-model="formData.complaintDeduction" placeholder="请输入投诉扣分" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="流失扣分" prop="lossDeduction">
            <el-input v-model="formData.lossDeduction" placeholder="请输入流失扣分" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="记账扣分" prop="accountingDeduction">
            <el-input v-model="formData.accountingDeduction" placeholder="请输入记账扣分" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="其他扣分" prop="otherDeduction">
            <el-input v-model="formData.otherDeduction" placeholder="请输入其他扣分" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="工作绩效考核扣分小计" prop="performanceDeductionSubtotal">
            <el-input v-model="formData.performanceDeductionSubtotal" placeholder="请输入工作绩效考核扣分小计" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="负责人给予考核分" prop="managerAssessmentScore">
            <el-input v-model="formData.managerAssessmentScore" placeholder="请输入负责人给予考核分" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="总分值" prop="totalScore">
            <el-input v-model="formData.totalScore" placeholder="请输入总分值" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="考核工资" prop="assessmentSalary">
            <el-input v-model="formData.assessmentSalary" placeholder="请输入考核工资" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="考核基数" prop="otherAdjustment">
            <el-input v-model="formData.otherAdjustment" placeholder="请输入考核基数" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="24">
          <el-form-item label="备注" prop="remarks">
            <el-input v-model="formData.remarks" type="textarea" row="5" placeholder="请输入备注" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { EmployeeScoreApi, EmployeeScoreVO } from '@/api/oa/employeescore'
import UserSelect from '@/views/system/user/UserSelect.vue'

/** 财务部员工分值考核 表单 */
defineOptions({ name: 'EmployeeScoreForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  years: undefined,
  month: undefined,
  userId: undefined,
  userName: undefined,
  totalEnterprises: undefined,
  smallScaleEnterprises: undefined,
  generalTaxpayerEnterprises: undefined,
  smallScaleScore: undefined,
  generalTaxpayerScore: undefined,
  simplifiedSmallScale: undefined,
  simplifiedGeneralTaxpayer: undefined,
  simplifiedSmallScaleScore: undefined,
  simplifiedGeneralTaxpayerScore: undefined,
  outsourcedSmallScale: undefined,
  outsourcedGeneralTaxpayer: undefined,
  outsourcedSmallScaleScore: undefined,
  outsourcedGeneralTaxpayerScore: undefined,
  invoiceScore: undefined,
  specialScore: undefined,
  seniorityScore: undefined,
  certificateScore: undefined,
  collectionCompletionRate: undefined,
  collectionDeduction: undefined,
  complaintDeduction: undefined,
  lossDeduction: undefined,
  accountingDeduction: undefined,
  otherDeduction: undefined,
  performanceDeductionSubtotal: undefined,
  managerAssessmentScore: undefined,
  totalScore: undefined,
  assessmentSalary: undefined,
  otherAdjustment: undefined,
  status: undefined,
  remarks: undefined,
})
const formRules = reactive({
  years: [{ required: true, message: '年份不能为空', trigger: 'blur' }],
  month: [{ required: true, message: '月份不能为空', trigger: 'blur' }],
  userId: [{ required: true, message: '员工id不能为空', trigger: 'blur' }],
  userName: [{ required: true, message: '员工姓名不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await EmployeeScoreApi.getEmployeeScore(id)
       formData.value.years = formData.value.years.toString() // 确保 years 被设置
      formData.value.month = formData.value.month < 10 ? '0' + formData.value.month.toString() : formData.value.month.toString() // 确保 month 被设置
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as EmployeeScoreVO
    if (formType.value === 'create') {
      await EmployeeScoreApi.createEmployeeScore(data)
      message.success(t('common.createSuccess'))
    } else {
      await EmployeeScoreApi.updateEmployeeScore(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    years: undefined,
    month: undefined,
    userId: undefined,
    userName: undefined,
    totalEnterprises: undefined,
    smallScaleEnterprises: undefined,
    generalTaxpayerEnterprises: undefined,
    smallScaleScore: undefined,
    generalTaxpayerScore: undefined,
    simplifiedSmallScale: undefined,
    simplifiedGeneralTaxpayer: undefined,
    simplifiedSmallScaleScore: undefined,
    simplifiedGeneralTaxpayerScore: undefined,
    outsourcedSmallScale: undefined,
    outsourcedGeneralTaxpayer: undefined,
    outsourcedSmallScaleScore: undefined,
    outsourcedGeneralTaxpayerScore: undefined,
    invoiceScore: undefined,
    specialScore: undefined,
    seniorityScore: undefined,
    certificateScore: undefined,
    collectionCompletionRate: undefined,
    collectionDeduction: undefined,
    complaintDeduction: undefined,
    lossDeduction: undefined,
    accountingDeduction: undefined,
    otherDeduction: undefined,
    performanceDeductionSubtotal: undefined,
    managerAssessmentScore: undefined,
    totalScore: undefined,
    assessmentSalary: undefined,
    otherAdjustment: undefined,
    status: undefined,
    remarks: undefined,
  }
  formRef.value?.resetFields()
}

const onUserChange = (user: any) => {
  formData.value.userId = user?.id
  formData.value.userName = user?.nickname
}
</script>